package com.lenovo.translatetext.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lenovo.translatetext.R;

import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * @ProjectName: TranslateText
 * @Package: com.lenovo.translatetext
 * @ClassName: LanguageListAdapter
 * @Description: LanguageListAdapter类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/4/22 11:15
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/4/22 11:15
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class LanguageListAdapter extends RecyclerView.Adapter<LanguageListAdapter.VH> {
    private List<String> mDatas;
    private Activity activity;


    public LanguageListAdapter(List<String> mDatas, Activity activity) {
        this.mDatas = mDatas;
        this.activity = activity;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_language_list_, viewGroup, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH vh, int i) {
        vh.language.setText(mDatas.get(i));
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击结束当前界面，返回语言名称
                //数据是使用Intent返回
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("chosenLanguage", mDatas.get(i));
                //设置返回数据
                activity.setResult(RESULT_OK, intent);
                //关闭Activity
                activity.finish();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder {
        public final TextView language;

        public VH(View v) {
            super(v);
            language = (TextView) v.findViewById(R.id.language);
        }
    }


}
